import * as mobx from 'mobx';
import { observable, action, useStrict, computed, asMap } from 'mobx';
import { overTimeSetting } from '../apis/req';

export class OverTimeStore{

    baseParams = {};
    columns = [];
    @observable nodeList = [];
    @observable nodeDatas = {};
    closeEvent = null;

    constructor(){
        this.setBaseParams = this.setBaseParams;
        this.doInit = this.doInit;
        this.doSave = this.doSave;
        this.changeDatas = this.changeDatas;
        this.setCloseEvent = this.setCloseEvent;
    }

    @action
    setBaseParams = (params={}) => {
        this.baseParams = params;
    }

    doInit = () => {
        overTimeSetting({...this.baseParams,...{opertype:"get"}}).then(data =>{
            this.nodeList = data.nodeList;
            this.nodeDatas = data.nodeDatas;
        });
    }

    doSave = () => {
        let nodeids = "";
        this.nodeList.length > 0 && this.nodeList.map((key,index) =>{
            nodeids += `${index > 0 ? "," : ""}${key}`;
        });
        let params = {opertype:"save", nodeids: nodeids};
        params = {...params, ...this.baseParams, ...mobx.toJS(this.nodeDatas)};
        overTimeSetting(params).then(data =>{
            this.closeEvent();
        });
    }

    changeDatas = (key, value) => {
        this.nodeDatas = {...this.nodeDatas, ...{[key]: value}};
    }

    setCloseEvent = (fun) => {
        this.closeEvent = fun;
    }
    
}